﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace JumpstartApp.Controls
{
    public class BindableTextBox : TextBox
    {
        public string BindableText
        {
            get { return (string)GetValue(BindableTextProperty); }
            set { SetValue(BindableTextProperty, value); }
        }

        public static readonly DependencyProperty BindableTextProperty =
            DependencyProperty.Register("BindableText", typeof(string),
            typeof(BindableTextBox), new PropertyMetadata(string.Empty, OnBindableTextChanged));

        private static void OnBindableTextChanged
            (DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
        {
            ((BindableTextBox)sender).OnBindableTextChanged(
                (string)eventArgs.OldValue, (string)eventArgs.NewValue);
        }

        public BindableTextBox()
        {
            TextChanged += (s, e) => BindableText = Text;
            GotFocus +=
                (s, e) => Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().ShowOnKeyboardInput = false;
            LostFocus +=
                (s, e) => Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;
        }

        private void OnBindableTextChanged(string oldValue, string newValue)
        {
            Text = newValue ?? string.Empty;
        }
    }
}
